package com.wanczy.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;

import com.wanczy.pojo.Goods;
import com.wanczy.pojo.OrderUserGoods;
import com.wanczy.pojo.Shop;
import com.wanczy.service.ShopService;
import com.wanczy.util.DateToString;
import com.wanczy.util.StringToDate;

@Controller
@SessionAttributes(value= {"shop"})
public class ShopController {
	@Autowired
	private ShopService shopService;
	
	//商户登录功能
	@RequestMapping(value="/shopLogin",method=RequestMethod.POST)
	public String shopLogin(@RequestParam("shopTel") String shopTel,@RequestParam("shopPass") String shopPass,Map<String,Object> map){
		Shop shop = shopService.checkShop(shopTel);
		map.put("shop", shop);
		if(shop == null) {//用户不存在时跳转
			return "redirect:/jsp/shop/NoTel.jsp";
		}else if(shop.getShopPass().equals(shopPass)==true) {//用户和密码都正确时跳转
			return "redirect:/jsp/shop/ShopBody.jsp";
		}else if(shop.getShopPass().equals(shopPass)==false) {//密码不正确时跳转
			return "redirect:/jsp/shop/PassError.jsp";
		}
		return "redirect:/jsp/shop/Notel.jsp";
	}
	
	//商品信息查询功能
	@RequestMapping(value="/ShopQuery")
	public String queryShopMessage(@RequestParam("shopId") Integer shopId,Map<String,Object> map) {
		Shop shop = shopService.FindByShopId(shopId);
		map.put("shopMessage",shop);
		return "/shop/ShopMessage";
	}
	
	//商户信息表单回显,辅助商户信息修改
	@RequestMapping(value="/ShopMessageUpd")
	public String shopMessageUpd(@RequestParam("shopId") Integer shopId,Map<String,Object> map) {
		Shop shop = shopService.FindByShopId(shopId);
		map.put("updMessage",shop);
		return "/shop/ShopMessageUpd";
	}
	
	//商户信息修改功能
	@RequestMapping(value="/doUpdShop",method=RequestMethod.PUT)
	public String shopUpd(Shop shop) {
		Integer count = shopService.updShopMessage(shop);
		if(count==1) {
			return "/shop/success";
		}
		return "/shop/fail";
	}
	
	//商户商品查询功能
	@RequestMapping(value="/ShopGoodsQuery")
	public String queryShopGoods(@RequestParam("shopId") Integer shopId,Map<String,Object> map) {
		List<Goods> list = shopService.findByShopId(shopId);
		map.put("Goods", list);
		return "/shop/ShopGoods";
	}
	
	//商户商品信息表单回显，辅助商户对自家商品的修改
	@RequestMapping(value="/ShowShopGoods",method=RequestMethod.GET)
	public String showShopGoods(@RequestParam("goodsId") Integer goodsId,Map<String,Object> map) {
		Goods goods = shopService.findByGoodsId(goodsId);
		map.put("showGoods", goods);
		return "/shop/ShopGoodsUpd";
	}
	
	//商品信息的修改
	@RequestMapping(value="/ShopGoodsUpd")
	public String ShopGoodsUpd(Goods goods,HttpServletRequest request,@RequestParam(value="file") MultipartFile file,
			HttpSession session) throws Exception{
		/*System.out.println("fileName :" + file.getName());
		System.out.println("OriginalFilename :" + file.getOriginalFilename());
		System.out.println("InputStream:" + file.getInputStream());*/
		String path = session.getServletContext().getRealPath("/images");
		File newFile = new File(path + File.separator + file.getOriginalFilename());
		// 转存文件到指定的路径
		file.transferTo(newFile);
		goods.setGoodsPhoto(file.getOriginalFilename());
		Integer count = shopService.updShopGoods(goods);
		if(count>0) {
			return "/shop/success";
		}
		return "/shop/fail";
	}
	
	//商户商品查询功能(辅助商品下架)
	@RequestMapping(value="/GoodsDown")
	public String queryGoods(@RequestParam("shopId") Integer shopId,Map<String,Object> map) {
		List<Goods> list = shopService.findByShopId(shopId);
		map.put("Goods", list);
		return "/shop/GoodsDown";
	}
	
	//商品下架功能
	@ResponseBody
	@RequestMapping(value="/downGoods",method=RequestMethod.PUT)
	public List<Integer> goodsDown(@RequestParam("goodsId") Integer goodsId) {
		List<Integer> list = new ArrayList<Integer>();
		Integer count = shopService.GoodsDown(goodsId);
		list.add(count);
		return list;
	}
	
	//商品重新上架功能
	@ResponseBody
	@RequestMapping(value="/upGoods",method=RequestMethod.PUT)
	public List<Integer> goodsUp(@RequestParam("goodsId") Integer goodsId) {
		List<Integer> list = new ArrayList<Integer>();
		Integer count = shopService.GoogsUp(goodsId);
		list.add(count);
		return list;
	}
	
	//商品删除功能
	@ResponseBody
	@RequestMapping(value="/delGoods",method=RequestMethod.DELETE)
	public List<Integer> goodsDel(@RequestParam("goodsId") Integer goodsId) {
		List<Integer> list = new ArrayList<Integer>();
		Integer count = shopService.GoogsDel(goodsId);
		list.add(count);
		return list;
	}
	
	//新增商品
	@RequestMapping(value="/ShopGoodsAdd",method=RequestMethod.POST)
	public String goodsAdd(Goods goods,@RequestParam("date") String date,@RequestParam("p") String p,
			@RequestParam("c") String c,@RequestParam("d") String d,HttpServletRequest request,
			@RequestParam(value="file") MultipartFile file,HttpSession session) throws Exception{
		String path = session.getServletContext().getRealPath("/images");
		File newFile = new File(path + File.separator + file.getOriginalFilename());
		// 转存文件到指定的路径
		file.transferTo(newFile);
		goods.setGoodsPhoto(file.getOriginalFilename());
		Date goodsDate = StringToDate.getDate(date);
		goods.setGoodsDate(goodsDate);
		String goodsAddress = p+c+d;
		goods.setGoodsAddress(goodsAddress);
		shopService.goodsAdd(goods);
		return "/shop/Addsuccess";
	}
	
	//商户订单查询
	@ResponseBody
	@RequestMapping(value="/queryOrder",method=RequestMethod.GET)
	public List<OrderUserGoods> queryShopOrder(@RequestParam("shopId") Integer shopId,@RequestParam("beginDay") String beginDay,
			@RequestParam("endDay") String endDay) throws Exception{
		Date begin = StringToDate.getDate(beginDay);
		Date end = StringToDate.getDate(endDay);
		List<OrderUserGoods> list = shopService.findOrder(shopId, begin, end);
		for (OrderUserGoods orderUserGoods : list) {
			String time =DateToString.getString(orderUserGoods.getOrderTime());
			orderUserGoods.setTime(time);
		}
		return list;
	}
	
	//商家发货功能
	@ResponseBody
	@RequestMapping(value="/fahuo",method=RequestMethod.PUT)
	public List<Integer> fahuo(@RequestParam("orderId") Integer orderId) {
		List<Integer> list = new ArrayList<Integer>();
		Integer count = shopService.fahuo(orderId);
		list.add(count);
		return list;
	}
	
	//新商户注册
	@ResponseBody
	@RequestMapping(value="/shopRegist",method=RequestMethod.POST)
	public int shopRegist(Shop shop) {
		String name = shop.getShopName();
		int count = 0;
		List<Shop> list = shopService.registCheck(name);
		if(list.isEmpty()) {
			count = shopService.shopRegist(shop);
		}else {
			count = 9;
		}
		return count;
	}
}
